# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

# 先看一下Y的变化曲线，判断是否和随着日期发生变化
data = pd.read_csv('../notebook/day.csv')

data_0 = data[data.yr == 0]["cnt"]
data_1 = data[data.yr == 1]["cnt"]

# print(data.describe())
print(data_0.describe())
print(data_1.describe())

import matplotlib.pyplot as plt
import seaborn as sns

# fig = plt.figure()
# sns.distplot(data.cnt.values, bins=30, kde=True)
# plt.xlabel('cnt', fontsize=12)
# plt.show()
#
## 展示X,Y曲线图
fig = plt.figure()
ax = sns.regplot(x="instant", y="cnt", data=data)
plt.show()

## 循环展示分布图 (区分是否是类型数据)
# for key in data.keys():
#     print(key)
#     if "dteday" == key :
#         continue;
#     fig = plt.figure()
#     sns.distplot(data[key].values, bins=30, kde=True)
#     plt.xlabel(key, fontsize=12)
#     plt.show()

## 热力图，查询相关性
# cols=data.columns
# Calculates pearson co-efficient for all combinations，通常认为相关系数大于0.5的为强相关
data = data.drop("dteday", axis=1)
data_corr = data.corr().abs()
plt.subplots(figsize=(13, 9))
sns.heatmap(data_corr,annot=True)
# Mask unimportant features
# sns.heatmap(data_corr, mask=data_corr < 1, cbar=False)
plt.show()


#Set the threshold to select only highly correlated attributes
threshold = 0.5
corr_list = []
size = data_corr.shape[0]

#Search for the highly correlated pairs
for i in range(0, size): #for 'size' features
    for j in range(i+1,size): #avoid repetition
        if (data_corr.iloc[i,j] >= threshold and data_corr.iloc[i,j] < 1) or (data_corr.iloc[i,j] < 0 and data_corr.iloc[i,j] <= -threshold):
            corr_list.append([data_corr.iloc[i,j],i,j]) #store correlation and columns index

#Sort to show higher ones first
s_corr_list = sorted(corr_list,key=lambda x: -abs(x[0]))

#Print correlations and column names
for v,i,j in s_corr_list:
    print ("%s and %s = %.2f" % (data.columns[i],data.columns[j],v))





